package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherTaskAllocationMapper;
import com.zjdiepu.www.ibiz.other.IOtherTaskAllocationService;
import com.zjdiepu.www.model.other.dto.OtherTaskAllocation;
import com.zjdiepu.www.model.other.ext.OtherTaskAllocationExt;

/**
 * Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月10日 18时57分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherTaskAllocationServiceImpl extends BaseService implements IOtherTaskAllocationService {
	public static final Logger logger = LoggerFactory.getLogger(OtherTaskAllocationServiceImpl.class);

	@Autowired
	private OtherTaskAllocationMapper otherTaskAllocationMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherTaskAllocation otherTaskAllocation, OperateType operType) {
		switch (operType) {
		case add:
			otherTaskAllocation.initInsertData(otherTaskAllocation.getModifier(), otherTaskAllocation.getModifierName());
			otherTaskAllocationMapper.insert(otherTaskAllocation);
			break;
		case edit:
			otherTaskAllocation.initUpdateData(otherTaskAllocation.getModifier(), otherTaskAllocation.getModifierName());
			// 影响的记录数
			int updateCount = otherTaskAllocationMapper.updateByPrimaryKeySelective(otherTaskAllocation);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherTaskAllocation.initDeleteData(otherTaskAllocation.getModifier(), otherTaskAllocation.getModifierName());
			// 影响的记录数
			int deleteCount = otherTaskAllocationMapper.updateByPrimaryKeySelective(otherTaskAllocation);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherTaskAllocation> selectByExt(OtherTaskAllocationExt otherTaskAllocationExt) {
		return otherTaskAllocationMapper.selectByExt(otherTaskAllocationExt);
	}
	
	@Override
	public int countByExt(OtherTaskAllocationExt otherTaskAllocationExt){
		return otherTaskAllocationMapper.countByExt(otherTaskAllocationExt);
	}
	
	@Override
	public OtherTaskAllocation selectById(String id) {
		return otherTaskAllocationMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherTaskAllocationMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherTaskAllocation otherTaskAllocation) {
		return otherTaskAllocationMapper.countRefined(otherTaskAllocation);
	}

	@Override
	public OtherTaskAllocation selectRefined(OtherTaskAllocation otherTaskAllocation) {
		return otherTaskAllocationMapper.selectRefined(otherTaskAllocation);
	}
	
	@Override
	public int deletedRefined(OtherTaskAllocation otherTaskAllocation) {
		return otherTaskAllocationMapper.deletedRefined(otherTaskAllocation);
	}

	@Override
	public boolean repeat(OtherTaskAllocation otherTaskAllocation) {
		int count = countRefined(otherTaskAllocation);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherTaskAllocation.getId())) {
			return true;
		}
		OtherTaskAllocation dbItem = selectRefined(otherTaskAllocation);
		if(null == dbItem) {
			return false;
		}
		return !otherTaskAllocation.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherTaskAllocationMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherTaskAllocation> otherTaskAllocations) {
		return otherTaskAllocationMapper.insertBatch(otherTaskAllocations);
	}
}
